﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SevenInvaders.common;

namespace SevenInvaders.LEVEL_AMANDA
{
    class Level_Amanda
    {
        AmandaPlayer amandaPlayer;

        Texture2D background;

        Song backgroundMusic;

        GameWindow window;

        public Level_Amanda(ContentManager content, GameWindow window)
        {
            this.window = window;

            amandaPlayer = new AmandaPlayer(content.Load<Texture2D>("LEVEL_AMANDA/spaceship_amanda"), new Vector2(400,600), 1, window, content);
            background = content.Load<Texture2D>("LEVEL_AMANDA/background");

            backgroundMusic = content.Load<Song>("LEVEL_AMANDA/musiquinha");

            for (int i = 0; i < 5; i++)
            {
                AmandaEnemy.list.Add(new AmandaEnemy(content.Load<Texture2D>("LEVEL_AMANDA/spaceship_do_mal"), new Vector2(120 * i, 120)));

            }
        }

        public void Update(GameTime gameTime, KeyboardState teclado, KeyboardState teclado_anterior)
        {
            //Verifica se o som esta tocando
            if (MediaPlayer.State != MediaState.Playing)
            {
                MediaPlayer.Play(backgroundMusic);
            }

            amandaPlayer.Update(gameTime, teclado, teclado_anterior);

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Update(gameTime);

                if (Shoot.list[i].PositionY < -Shoot.list[i].Texture.Height)
                {
                    Shoot.list[i].Visible = false;
                }

                for (int j = 0; j < AmandaEnemy.list.Count; j++)
                {
                    if (Shoot.list[i].Collision.Intersects(AmandaEnemy.list[j].Collision)
                        && Shoot.list[i].Visible && AmandaEnemy.list[j].Visible)
                    {
                        AmandaEnemy.list[j].Visible = false;
                        Shoot.list[i].Visible = false;
                    }
                }
                
            }

            for (int i = 0; i < AmandaEnemy.list.Count; i++)
            {
                AmandaEnemy.list[i].Update(gameTime);
            }

            for (int i = 0; i < AmandaEnemy.list.Count; i++)
            {
                if (!AmandaEnemy.list[i].Visible)
                {
                    AmandaEnemy.list.RemoveAt(i);
                }
            }

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                if (!Shoot.list[i].Visible)
                {
                    Shoot.list.RemoveAt(i);
                }
            }

            
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(background, Vector2.Zero, Color.White);

            amandaPlayer.Draw(gameTime, spriteBatch);

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Draw(gameTime, spriteBatch);
            }

            amandaPlayer.Draw(gameTime, spriteBatch);

            for (int i = 0; i < AmandaEnemy.list.Count; i++)
            {
                AmandaEnemy.list[i].Draw(gameTime, spriteBatch);
            }
            
        }
    }
}
